package com.cn.news.controller;
import com.cn.common.pojo.NewsModel;
import com.cn.common.pojo.ResultModel;
import com.cn.news.service.NewsService;
import io.swagger.annotations.Api;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "新闻信息")
@RestController
@RequestMapping("news")
public class NewsController {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String date= DATE_FORMAT.format(new Date());
    @Autowired
    NewsService newsService;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @GetMapping("newsForId")
    public  ResultModel newsForId(int id){
        ResultModel model = newsService.newsForId(id);
        return model;
    }
    @GetMapping("newsForNews")
    public ResultModel newsForNews(){
        ResultModel newsForNews = newsService.newsForNews();
        return newsForNews;
    }
    @GetMapping("newsByType")
    public ResultModel newsByType(int type){
        List<NewsModel> models = newsService.newsByType(type);
        return ResultModel.Data(models);
    }

    @GetMapping("news")
    public ResultModel news(){
        ResultModel news = newsService.news();
        return news;
    }
    @GetMapping("delete")
    public ResultModel delete(int id){
        ResultModel delete = newsService.delete(id);
        rabbitTemplate.convertAndSend("userTopicExchange","leader.hello","{reporter:'大哥好',date:\'"+date+"\',content:'一条新闻已被删除'}");
        return delete;
    }
    @PostMapping("update")
    public ResultModel update(NewsModel newsModel){
        ResultModel update = newsService.update(newsModel);
        rabbitTemplate.convertAndSend("userTopicExchange","leader.hello","{reporter:'大哥好',date:\'"+date+"\',content:'新闻信息已被修改'}");
        return update;
    }
    @PostMapping("insert")
    public ResultModel insert(NewsModel newsModel){
        ResultModel insert = newsService.insert(newsModel);
        rabbitTemplate.convertAndSend("userTopicExchange","leader.hello","{reporter:'大哥好',date:\'"+date+"\',content:'增加了一条新闻信息'}");
        return insert;
    }
    @PostMapping(value = "enable")
    public ResultModel enable(int id){
        ResultModel enable = newsService.enable(id);
        return enable;
    }
    @GetMapping("findById")
    public ResultModel findById(int id){
        ResultModel findById = newsService.findById(id);
        return findById;
    }
    @GetMapping("limit")
    public ResultModel getCompan(NewsModel newsModel,int limit,int page){
        newsModel.setPageNum(page);
        newsModel.setPageSize(limit);
        ResultModel resultModel = newsService.limit(newsModel);
        return resultModel;
    }
}
